﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crosland.Automotriz.Core;
using Crosland.Automotriz.Entidad;
namespace Crosland.Automotriz.Data.Dao.Linq
{
    public class clsT_Mae_Tabla_General_DetDAO
    {

        private AutomotrizEntities ContextoDeObjs = new AutomotrizEntities();

        public IEnumerable<Object> lista(int id_general_cab)
        {

        IEnumerable<Object> list = new List<Object>();

        try
        {

            var q = from d in ContextoDeObjs.T_MAE_TABLA_GENERAL_DET
                    from c in ContextoDeObjs.T_MAE_TABLA_GENERAL_CAB
                    where c.ID_GENERAL_CAB == d.ID_GENERAL_CAB && d.ID_GENERAL_CAB==id_general_cab

                    select new
                    {
                        d.ID_GENERAL_DET,
                        d.ID_GENERAL_CAB,
                        d.DES_CORTA,
                        d.DES_LARGA,
                        d.IND_ACTIVO,
                        c.NOM_TABLA,
                        d.IMP_VALOR,
                        d.DES_LARGA_OPCIONAL,
                    };



            list = q.ToList();
        }
        catch{}

        return list;

        
    }

        public int IDSetup(string DES_LARGA) 
        {
            try
            {
                var q = from d in ContextoDeObjs.T_MAE_TABLA_GENERAL_DET
                        where d.DES_LARGA == DES_LARGA
                        select d;
                return q.ToList()[0].ID_GENERAL_DET;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public List<Crosland.Automotriz.Entidad.T_MAE_TABLA_GENERAL_DET> Listar(string NOM_TABLA, int ID_MODULO)
        {
            DM_CROSLAND_20130417Entities ContextoDeObjsEntidad = new DM_CROSLAND_20130417Entities();

            List<Crosland.Automotriz.Entidad.T_MAE_TABLA_GENERAL_DET> lista = new List<Crosland.Automotriz.Entidad.T_MAE_TABLA_GENERAL_DET>();
            try
            {
                var results = (from d in ContextoDeObjsEntidad.T_MAE_TABLA_GENERAL_DET
                               join c in ContextoDeObjsEntidad.T_MAE_TABLA_GENERAL_CAB
                                on d.ID_GENERAL_CAB equals c.ID_GENERAL_CAB
                                where (c.NOM_TABLA == NOM_TABLA && c.ID_MODULO == ID_MODULO)
                                orderby d.DES_LARGA ascending
                                select new
                                {
                                    d.ID_GENERAL_DET,
                                    d.DES_CORTA,
                                    d.DES_LARGA
                                }).Distinct();

                foreach (var myEntity in results.Distinct().ToArray())
                {
                    Crosland.Automotriz.Entidad.T_MAE_TABLA_GENERAL_DET u = new Crosland.Automotriz.Entidad.T_MAE_TABLA_GENERAL_DET();
                    u.ID_GENERAL_DET = myEntity.ID_GENERAL_DET;
                    u.DES_CORTA = myEntity.DES_CORTA;
                    u.DES_LARGA = myEntity.DES_LARGA;
                    lista.Add(u);
                }
            }
            catch (Exception)
            {

            }
            return lista;
        }

        public bool Insertar(Crosland.Automotriz.Core.T_MAE_TABLA_GENERAL_DET Obj)
    {
        try
        {
            ContextoDeObjs.AddObject("T_MAE_TABLA_GENERAL_DET", Obj);
            ContextoDeObjs.SaveChanges();
        }
        catch (Exception)
        {
            return false;
        }
        return true;
    }

        public bool Modificar(Crosland.Automotriz.Core.T_MAE_TABLA_GENERAL_DET obj)
    {
        try
        {
            ContextoDeObjs.AttachTo("T_MAE_TABLA_GENERAL_DET", obj);
            ContextoDeObjs.ObjectStateManager.ChangeObjectState(obj, System.Data.EntityState.Modified);
            ContextoDeObjs.SaveChanges();
        }
        catch (Exception)
        {
            return false;

        }
        return true;
    }

        public bool Eliminar(Crosland.Automotriz.Core.T_MAE_TABLA_GENERAL_DET obj)
    {
        try
        {
            ContextoDeObjs.AttachTo("T_MAE_TABLA_GENERAL_DET", obj);
            ContextoDeObjs.DeleteObject(obj);
            ContextoDeObjs.SaveChanges();
        }
        catch (Exception)
        {
            return false;
        }
        return true;
    }

        public int UltimoNumero()
    {
        int max = 0;

        try
        {
            max = ContextoDeObjs.T_MAE_TABLA_GENERAL_DET.Select(x => x.ID_GENERAL_DET).Max();

        }
        catch (Exception)
        {
            return max;
        }

        return max;

    }

        public IEnumerable<Object> listarFiltroTipoUsuario() {

            IEnumerable<Object> lista = new List<Object>();

            try
            {
                var q = from id in ContextoDeObjs.T_MAE_TABLA_GENERAL_DET 
                        from ic in ContextoDeObjs.T_MAE_TABLA_GENERAL_CAB
                        where ic.ID_GENERAL_CAB==id.ID_GENERAL_CAB
                        && ic.NOM_TABLA == "TIPO DE USUARIO" && id.IND_ACTIVO == true
                        select new{

                                    id.ID_GENERAL_DET,
                                    id.DES_CORTA,
                                    id.DES_LARGA
                                };
                lista = q.ToList();
            }
            catch
            {
               
            }
            return lista;
        }










        public IEnumerable<Object> listaArea()
        {

            IEnumerable<Object> list = new List<Object>();
            try
            {

                var q = from c in ContextoDeObjs.T_MAE_TABLA_GENERAL_CAB
                        from d in ContextoDeObjs.T_MAE_TABLA_GENERAL_DET
                        where c.ID_GENERAL_CAB == d.ID_GENERAL_CAB
                        && c.NOM_TABLA == "T_MAE_AREA_NEGOCIO"
                        select new
                        {
                            d.ID_GENERAL_DET,
                            d.DES_CORTA,
                            d.ID_GENERAL_CAB
                        };

                list = q.ToList();
            }
            catch
            {

            }

            return list;
        }









        public IEnumerable<Object> ListarUsuariosCorreo(int idCab)
        { 
        
            IEnumerable<Object> lista=new List<Object>();

            var q=from id in ContextoDeObjs.T_MAE_TABLA_GENERAL_DET
                  join u in ContextoDeObjs.V_Usuario
                  on id.DES_CORTA equals u.ID_USUARIO
                  where id.ID_GENERAL_CAB==idCab
                  select new
                  {
                    ID_USUARIO=id.DES_CORTA,
                    NOMBRE_APELLIDO=u.DES_NOMBRE + " " + u.DES_APELLIDOS,
                    CORREO=id.DES_LARGA
                  };

            lista=q.ToList();

            return lista;
        }


    }
    
}
